<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="css/base.css">
    <link rel="stylesheet" href="css/index.css">
</head>

<body>
    <section id="todoapp" class="todoapp">
        <header class="header">
            <h1>todos</h1>
            <input placeholder="What needs to be done?" @keyup.enter="addTodo" class="new-todo">

        </header>
        <section class="main">
            <input v-model="toggleStat" id="toggle-all" type="checkbox" class="toggle-all">
            <label for="toggle-all">Mark all as complete</label>
            <ul class="todo-list">

                <li v-for="(item, index) in todos" v-bind:class="{completed: item.completed, editing: item === currentEditing}">
                    <div class="view">
                        <input type="checkbox" class="toggle" v-model="item.completed">
                        <label @dblclick="currentEditing = item">{{item.title}}</label>
                        <button class="destroy" @click="removeTodo(index, $event)"></button>
                    </div>
                    <input class="edit" @keyup.enter="saveEdit(item, index, $event)" :value="item.title" @keyup.esc="currentEditing = null">
                </li>
            </ul>
        </section>
        <footer class="footer">
            <span class="todo-count"><strong>{{leftCount}}</strong> item left</span>
            <ul class="filters">
                <li><a href="#/" class="selected">All</a></li>
                <li><a href="#/active" class="">Active</a></li>
                <li><a href="#/completed" class="">Completed</a></li>
            </ul> <button class="clear-completed">Clear completed</button></footer>
    </section>
    <script src="js/vue.js"></script>

    <script>
        new Vue({
            el: "#todoapp",
            data: {
                currentEditing: null,
                todos: [{
                    id: 1,
                    title: '吃饭',
                    completed: false
                }, {
                    id: 2,
                    title: '睡觉',
                    completed: false
                }, {
                    id: 3,
                    title: '打豆豆',
                    completed: true
                }]
            },
            methods: {
                addTodo(event) {
                    // 1. 获取文本框中用户输入的数据
                    // 2. 判断数据是否非空
                    //    如果是空的，则什么都不做
                    //    如果不是空的，则添加到数组中
                    // 3. 添加到数组中
                    // 4. 清空文本框
                    var todoText = event.target.value.trim()
                    if (!todoText.length) {
                        return
                    }

                    const lastTodo = this.todos[this.todos.length - 1]
                    const id = lastTodo ? lastTodo.id + 1 : 1

                    // 当数组发生变化，则绑定渲染该数组的视图也会得到更新
                    this.todos.push({
                        id,
                        title: todoText,
                        completed: false
                    })

                    // 清空文本框
                    event.target.value = ''
                },
                // 删除任务项
                removeTodo(delIndex) {
                    this.todos.splice(delIndex, 1)
                },
                // 保存编辑项
                saveEdit(item, index, event) {
                    // 1. 拿到文本框中的数据
                    //    非空校验
                    //    如果为空，则直接删除这个 item
                    //    如果不为空，则修改任务项的 title 数据
                    var editText = event.target.value.trim()

                    // 程序员要具有工匠精神：优化简写
                    // !editText.length ?
                    //   this.todos.splice(index, 1) :
                    //   item.title = editText

                    if (!editText.length) {
                        // 将元素直接从数组中移除
                        return this.todos.splice(index, 1)
                    }

                    // 2. 将数据设置到任务项中
                    item.title = editText

                    // 3. 去除 editing 样式
                    this.currentEditing = null
                },
            },
            computed: {
                toggleStat: {
                    get() {
                        console.log(2345)
                        return this.todos.every(item => item.completed)
                    },
                    set(val) {
                        console.log(678)
                        console.log(val)
                        this.todos.forEach(todo => todo.completed = val)
                    }
                },
                leftCount: function() {
                    return this.todos.filter(item => !item.completed).length
                },

            }
        })
    </script>
</body>

</html>